home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / libcairo-perl / examples / png / caps_joins.pl < prev    next >
Encoding:
Perl Script  |  2006-05-14  |  1.4 KB  |  81 lines

  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Cairo;
  6.  
  7. use constant
  8. {
  9.     WIDTH => 600,
  10.     HEIGHT => 600,
  11. };
  12.  
  13. {
  14.     my $surf = Cairo::ImageSurface->create ('argb32', WIDTH, HEIGHT);
  15.     my $cr = Cairo::Context->create ($surf);
  16.  
  17.     $0 =~ /(.*)\.pl/;
  18.     my $out = "$1.png";
  19.  
  20.     $cr->rectangle (0, 0, WIDTH, HEIGHT);
  21.     $cr->set_source_rgb (1, 1, 1);
  22.     $cr->fill;
  23.  
  24.     draw_caps_joins ($cr, WIDTH, HEIGHT);
  25.  
  26.     $cr->show_page;
  27.  
  28.     $surf->write_to_png ($out);
  29. }
  30.  
  31. sub stroke_v_twice
  32. {
  33.     my ($cr, $width, $height) = @_;
  34.  
  35.     $cr->move_to (0, 0);
  36.     $cr->rel_line_to ($width / 2, $height / 2);
  37.     $cr->rel_line_to ($width / 2, - $height / 2);
  38.  
  39.     $cr->save;
  40.     $cr->stroke;
  41.     $cr->restore;
  42.  
  43.     $cr->save;
  44.     {
  45.         $cr->set_line_width (2.0);
  46.         $cr->set_line_cap ('butt');
  47.         $cr->set_source_rgb (1, 1, 1);
  48.         $cr->stroke;
  49.     }
  50.     $cr->restore;
  51.  
  52.     $cr->new_path;
  53. }
  54.  
  55. sub draw_caps_joins
  56. {
  57.     my ($cr, $width, $height) = @_;
  58.  
  59.     my $line_width = $height / 12 & (~1);
  60.  
  61.     $cr->set_line_width ($line_width);
  62.     $cr->set_source_rgb (0, 0, 0);
  63.  
  64.     $cr->translate ($line_width, $line_width);
  65.     $width -= 2 * $line_width;
  66.  
  67.     $cr->set_line_join ('bevel');
  68.     $cr->set_line_cap ('butt');
  69.     stroke_v_twice ($cr, $width, $height);
  70.  
  71.     $cr->translate (0, $height / 4 - $line_width);
  72.     $cr->set_line_join ('miter');
  73.     $cr->set_line_cap ('square');
  74.     stroke_v_twice ($cr, $width, $height);
  75.  
  76.     $cr->translate (0, $height / 4 - $line_width);
  77.     $cr->set_line_join ('round');
  78.     $cr->set_line_cap ('round');
  79.     stroke_v_twice ($cr, $width, $height);
  80. }
  81.